var ImageMsgSender = (function(){
    
    var _ImageMsgSender = (function(){}).extend(MsgSender);
    
    _ImageMsgSender.prototype.send = function(cb){
        var _this = this, imgMeta = this.content;
        
        var chatGroupEntity = page.chatGroupStore.getItem(_this.chatGroupID);
        var localImgUrl = URL.createObjectURL(imgMeta.imgFile);
        
        ujs.browser.img.getSize(imgMeta.imgFile).done(function(w, h){
            
            var imgData = {
                contentType : 'image',                            
                teid : _this.chatGroupID,                                
                img : localImgUrl,
                originalImg : localImgUrl,
                content : {
                    small : [imgMeta.width, imgMeta.height],
                    big : [w, h]
                }
            };
            
            if(chatGroupEntity.perm.rloc && page.position){
                imgData['la'] = page.position.coords.latitude;
                imgData['lo'] = page.position.coords.longitude;
            }
            
            //呈现ImageMsg,带进度条.
            var imgMsg = (new ChatImageMsg(imgData, 'right')).render(_this.chatPanel.chatView).showProgressBar();
            imgMsg.markSending();
            
            var flag = false;
            if(imgMsg.canShowReaders()){
                flag = true;
                imgMsg.disableShowReaders();
            }
           
            var imgGuid = String.newGuid();
            var tImgGuid = String.newGuid();
            
            var json = {
                type : 'image', 
                teid : _this.chatGroupID, 
                content : {
                    rid : '{{0}}'.format(imgGuid), 
                    trid : '{{0}}'.format(tImgGuid), 
                    length : imgMeta.imgFile.size,
                    name : imgMeta.imgFile.name,
                    small : [imgMeta.width, imgMeta.height],
                    big : [w, h]
                }
            };             
            var imgData = new FormData();
            imgData.append('topic', JSON.stringify(json));                                             
            imgData.append(imgGuid, imgMeta.imgFile);            
            imgData.append( tImgGuid, imgMeta.thumbImgFile, imgMeta.imgFile.name); 
            
            //--------------------------------------------------------------------------------
            
            var uploadHelper = ujs.browser.UploadHelper.create({
                url : '/apis/chat/{0}/uploadex?type={1}'.format(_this.chatGroupID, MsgType.IMAGE),              
                formData : imgData
            });
            //进度.
            uploadHelper.on('progress', function(percentComplete){
                imgMsg.progressBar.setValue(percentComplete.toFixed(0));
            });
            uploadHelper.on('success', function(msg){
                var topic = msg['topic'];
                var content = topic['content'];
                imgMsg.clearSending();
                page.chatMsgStore.addItem({
                    local : true,
                    teid : _this.chatGroupID,
                    seq : topic['seq'],
                    createTime : topic['createTime'],
                    contentType : MsgType.IMAGE,
                    from : 'web',
                    id : topic['id'],
                    content : {
                        name : imgMeta.imgFile.name,
                        length : imgMeta.imgFile.size,
                        trid : content['trid'],
                        rid : content['rid'],
                        small : [imgMeta.width, imgMeta.height],
                        big : [w, h]
                    },
                    user : _this.user
                });
                imgMsg.updateSeq(topic['seq']).updateMsgTime(topic['createTime']);
                imgMsg.adjustPos(topic['seq']).chatView.refreshReadCount();
                imgMsg.adjustPos(topic['seq']).chatView.scrollToEnd();
                if(flag){
                    imgMsg.enableShowReaders();
                }
                cb && cb(topic);
            });
            
            uploadHelper.on('complete', function(){
                //firefox progress always not reach 100%, so force 100% when complete.                                
                imgMsg.progressBar.setValue(100);
                imgMsg.hideProgressBar();
                if(_this.chatPanel.chatGroupEntity.type == 'singlechat'){
                    imgMsg.setUnread();
                }
            });
            uploadHelper.on('fail', function(){
                imgMsg.markFailed();
                imgMsg.$retryBtn.click(function(){
                    imgMsg.hide();
                    _this.send(function(){
                        _this.chatPanel.chatView.$wrapper.scrollToEnd();
                    });
                    return false;
                });
            });     
            //启动上传图片消息.  
            uploadHelper.startAjaxUpload();   
        });
    };
    
    return _ImageMsgSender;
})();
